﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 安装包版本读取 : System.Web.UI.Page
{
    /// <summary>
    /// 工作步骤
    /// 1.扫描目录,及子目录下,是否存在packeg.txt 如果存在则表示此目录为安装包加,版本说明形式
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        PackegTxt();
        //数据邦定
        repdir.ItemDataBound += (s, ev) =>
        {
            var itemsource = ev.Item.DataItem as fieldSource;
            Repeater rep = (ev.Item.FindControl("rep1") as Repeater);
            rep.DataSource = itemsource.List;
            rep.DataBind();
        };
        repdir.DataSource = Source;
        repdir.DataBind();
    }
    /// <summary>
    ///
    /// 		incomV4.0.0.3-20140902.rar	--压缩包
    ///	    incomV4.0.0.3-20140902.txt	--此版本功能或者更新描述说明
    /// </summary>
    public void PackegTxt()
    {
        //1.按版版本名称排序.将最新文件放到最前.
        //2.输出说明日志.
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(Server.MapPath("~/"));

        EachFiles(info);
    }
    /// <summary>
    /// 邦定数据源
    /// </summary>
    public List<fieldSource> Source = new List<fieldSource>();

    /// <summary>
    /// 字段资源
    /// </summary>
    public class fieldSource
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 数据集合
        /// </summary>
        public List<Fsdic> List { get; set; }

    }

    /// <summary>
    /// 内容 
    /// </summary>
    public class Fsdic
    {
        public string Content { get; set; }
        public string Name { get; set; }
    }


    /// <summary>
    /// 遍历所有文件
    /// </summary>
    public void EachFiles(System.IO.DirectoryInfo info)
    {
        //检查是否存在文件
        //1.packeg.txt  安装包
        //2.sdkdoc.txt  SDK文档
        //3.apidoc.txt  api远程调用文档

        var ifles = info.GetFiles().OrderByDescending(p => p.Name).ToList();
        string type = ifles.Where(p =>
            p.FullName.ToLower().Contains("packeg.txt")
            ||
             p.FullName.ToLower().Contains("sdkdoc.txt")
             ||
              p.FullName.ToLower().Contains("apidoc.txt")
            ).Select(p => p.Name.ToLower()).FirstOrDefault();
        type = string.IsNullOrEmpty(type) ? string.Empty : type;
        switch (type)
        {
            //1.packeg.txt  安装包
            case "packeg.txt":
                #region 安装包
                fieldSource source = new fieldSource()
                {
                    List = new List<Fsdic>(),
                    Name = info.FullName.Replace(Server.MapPath("~/"), ""),
                };
                Source.Add(source);
                ifles.ForEach(fi =>
                {
                    //读取版本号字符串
                    if (fi.Extension.ToLower() != ".txt" && fi.Name.ToLower() != "packeg.txt")
                    {
                        //检查此文件下是否有 txt
                        if (System.IO.File.Exists(fi.FullName + ".txt"))
                        {
                            //处理txt文件
                            System.IO.FileInfo f = new System.IO.FileInfo(fi.FullName + ".txt");
                            List<string> strs = new List<string>();
                            string naem = f.Name.Substring(0, f.Name.Length - f.Extension.Length);
                            strs.Add(System.IO.File.ReadAllText(f.FullName).Replace("\r", "<br/>"));
                            strs.Add("<br/>");
                            strs.Add("<a href='" + f.FullName.Substring(0, f.FullName.Length - f.Extension.Length).Replace(Server.MapPath("~/"), "/") + "' target='blank'>" + naem + "</a>");
                            strs.Add("<hr/>");
                            source.List.Add(new Fsdic()
                            {
                                Name = naem,
                                Content = string.Join("", strs)
                            });
                        }
                        else {
                         //处理无版本说明的文件
                            List<string> strs = new List<string>();
                            string naem = fi.Name;
                            strs.Add("<a href='" + fi.FullName.Replace(Server.MapPath("~/"), "/") + "' target='blank'>" + naem + "</a>");
                            strs.Add("<hr/>");
                            source.List.Add(new Fsdic()
                            {
                                Name = naem,
                                Content = string.Join("", strs)
                            });
                        }
                    }
                });
                #endregion
                break;

            //2.sdkdoc.txt  SDK文档
            case "sdkdoc.txt":
                #region sdk文档用dll的xml生成


                #endregion
                break;

            //3.apidoc.txt  api远程调用文档
            case "apidoc.txt":

                break;
        }



        //遍历文件夹
        foreach (var dir in info.GetDirectories())
        {
            EachFiles(dir);
        }
    }
    /// <summary>
    /// 文件处理
    /// </summary>
    /// <param name="files"></param>
    protected void FileOper(IEnumerable<System.IO.FileInfo> files)
    {
        foreach (var fil in files)
        {

        }
    }
}